/**
 * @file setting_volte_switch.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_VOLTE_SWTICH != 0

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/


/**********************
 *  STATIC PROTOTYPES
 **********************/
static lv_obj_t * volte_switch_create(lv_obj_t * activity_obj);

/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
void volte_switch_create_btn_action(lv_obj_t * btn, lv_event_t event)
{
    (void)btn;

    if(LV_EVENT_CLICKED != event) return;
    lv_watch_png_cache_all_free();

    lv_obj_t * obj = volte_switch_create(NULL);
    LV_ASSERT_MEM(obj);
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static lv_volteswitch_obj_ext_t * volte_switch_get_ext(void)
{
    lv_volteswitch_obj_ext_t  * ext = NULL;
    lv_obj_t * watch_obj = NULL;

    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_VOLTE_SWITCH);
    if(activity_obj) {
        lv_watch_get_child_obj(activity_obj, lv_watch_obj_signal, &watch_obj);
    }
    if(watch_obj) {
        ext = lv_obj_get_ext_attr(watch_obj);
    }
    return ext;
}

static void volte_swtich_no_btn_action(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;

    lv_volteswitch_obj_ext_t * ext = volte_switch_get_ext();
    lv_obj_set_hidden(lv_obj_get_parent(ext->cb_on), false);
    lv_checkbox_set_checked(ext->cb_off, false);
    lv_obj_del(lv_obj_get_parent(btn));
}

static void volte_swtich_yes_btn_action(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;

    lv_volteswitch_obj_ext_t * ext = volte_switch_get_ext();
    watch_modem_set_volte_state_req(0);
    lv_obj_set_hidden(lv_obj_get_parent(ext->cb_on), false);
    lv_checkbox_set_checked(ext->cb_off, true);
    lv_checkbox_set_checked(ext->cb_on, false);
    lv_obj_del(lv_obj_get_parent(btn));
}

static void volte_switch_tip_create(lv_obj_t * par)
{
    lv_volteswitch_obj_ext_t * ext = volte_switch_get_ext();
    lv_obj_set_hidden(lv_obj_get_parent(ext->cb_on), true);
    lv_obj_t * cont = lv_cont_create(par, NULL);
    lv_obj_set_size(cont, 200, 180);
    lv_obj_set_pos(cont, 20, 40);
    lv_obj_add_style(cont, LV_CONT_PART_MAIN, &lv_watch_cont_opa1);

    lv_obj_t * label = lv_label_create(cont, NULL);
    lv_label_set_text_id(label, WATCH_TEXT_ID_AFFECT_CALL);
    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font20);
    lv_label_set_long_mode(label, LV_LABEL_LONG_BREAK);
    lv_obj_set_width(label, 160);
    lv_obj_align(label, NULL, LV_ALIGN_IN_TOP_MID, 0, 10);

    lv_obj_t * btn_no = lv_imgbtn_create(cont, NULL);
    lv_imgbtn_set_src(btn_no, LV_BTN_STATE_RELEASED, ICON_CANCEL);
    lv_imgbtn_set_src(btn_no, LV_BTN_STATE_PRESSED, ICON_CANCEL);
    lv_obj_align(btn_no, NULL, LV_ALIGN_IN_BOTTOM_LEFT, 10, -10);
    lv_obj_set_event_cb(btn_no, volte_swtich_no_btn_action);
    lv_watch_obj_add_element(btn_no);

    lv_obj_t * btn_yes = lv_imgbtn_create(cont, NULL);
    lv_imgbtn_set_src(btn_yes, LV_BTN_STATE_RELEASED, ICON_OK);
    lv_imgbtn_set_src(btn_yes, LV_BTN_STATE_PRESSED, ICON_OK);
    lv_obj_align(btn_yes, NULL, LV_ALIGN_IN_BOTTOM_RIGHT, -10, -10);
    lv_obj_set_event_cb(btn_yes, volte_swtich_yes_btn_action);
    lv_watch_obj_add_element(btn_yes);
}

static void volte_switch_prepare_destory(lv_obj_t * activity_obj)
{
    lv_watch_png_cache_all_free();
}

static void volte_switch_on_btn_action(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;

    lv_volteswitch_obj_ext_t * ext = volte_switch_get_ext();
    if(0 == ext->lv_watch_obj.element_click_en) return;

    lv_checkbox_set_checked(ext->cb_on, true);
    lv_checkbox_set_checked(ext->cb_off, false);

    if(MMI_Modem_Get_Volte_State_Req(watch_modem_get_sim_id()) == 0) {
        watch_modem_set_volte_state_req(1);
    }
}

static void volte_switch_off_btn_action(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;

    if(MMI_Modem_Get_Volte_State_Req(watch_modem_get_sim_id())) {
        lv_obj_t * par = lv_obj_get_parent(lv_obj_get_parent(btn));
        volte_switch_tip_create(par);
    } else {
        lv_checkbox_set_checked(btn, true);
    }
}

static lv_obj_t * volte_switch_create(lv_obj_t * activity_obj)
{
    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_VOLTE_SWITCH;
        activity_ext.create = volte_switch_create;
        activity_ext.prepare_destory = volte_switch_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(activity_obj == NULL) return NULL;
    }

    lv_obj_t * obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) return NULL;
    lv_volteswitch_obj_ext_t * ext = lv_obj_allocate_ext_attr(obj, sizeof(lv_volteswitch_obj_ext_t));
    LV_ASSERT_MEM(ext);

    lv_obj_t * label = lv_label_create(obj, NULL);
    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font20_gray);
    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    lv_obj_set_click(label, false);
    lv_label_set_text_id(label, WATCH_TEXT_ID_VOLTE);
    lv_obj_align(label, obj, LV_ALIGN_IN_TOP_MID, 0, 10);

    lv_obj_t * cont = lv_cont_create(obj, NULL);
    lv_obj_add_style(cont, LV_OBJ_PART_MAIN, &lv_style_transp_fit);
    if(lv_obj_get_base_dir(obj) == LV_BIDI_DIR_RTL)
        lv_btn_set_layout(cont, LV_LAYOUT_COLUMN_RIGHT);
    else
        lv_btn_set_layout(cont, LV_LAYOUT_COLUMN_LEFT);
    lv_obj_align(cont, NULL, LV_ALIGN_CENTER, -10, -20);
    lv_cont_set_fit2(cont, LV_FIT_TIGHT, LV_FIT_TIGHT);
    lv_obj_set_click(cont, false);

    lv_obj_t * cb_on = lv_checkbox_create(cont, NULL);
    lv_checkbox_set_text(cb_on, lv_lang_get_text(WATCH_TEXT_ID_ON));
    lv_obj_clean_style_list(cb_on, LV_CHECKBOX_PART_BULLET);
    lv_obj_add_style(cb_on, LV_CHECKBOX_PART_BULLET, &lv_watch_cb_styles);
    lv_obj_set_event_cb(cb_on, volte_switch_on_btn_action);
    lv_watch_obj_add_element(cb_on);

    lv_obj_t * cb_off = lv_checkbox_create(cont, cb_on);
    lv_checkbox_set_text(cb_off, lv_lang_get_text(WATCH_TEXT_ID_OFF));
    lv_obj_set_event_cb(cb_off, volte_switch_off_btn_action);
    lv_watch_obj_add_element(cb_off);

    if(MMI_Modem_Get_Volte_State_Req(watch_modem_get_sim_id())) {
        lv_checkbox_set_checked(cb_on, true);
    } else {
       lv_checkbox_set_checked(cb_off, true);
    }

    ext->cb_on = cb_on;
    ext->cb_off = cb_off;
    return obj;
}
#endif /*USE_LV_WATCH_VOLTE_SWTICH*/
